package sh.wn36.vehiclemanage.xzh;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sh.wn36.vehiclemanage.entity.Car;
import sh.wn36.vehiclemanage.entity.Dictionary;
import sh.wn36.vehiclemanage.entity.RepairRecord;
import sh.wn36.vehiclemanage.service.*;
import sh.wn36.vehiclemanage.service.impl.CarServiceImpl;
import sh.wn36.vehiclemanage.util.Result;
import sh.wn36.vehiclemanage.vo.driverInfo;

import javax.xml.transform.OutputKeys;
import java.util.List;

@CrossOrigin
@Api("维修记录")
@RestController
@RequestMapping("/repair")
public class Repair_Record_Controller {
    @Autowired
    private CarServiceImpl carService;
    @Autowired
    private CurrentUnitService currentUnitService;
    @Autowired
    private QUsersService qUsersService;
    @Autowired
    private RepairRecordService repairRecordService;
    @Autowired
    private DictionaryService dictionaryService;

    @ApiOperation("获取维修记录列表")
    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result getRepairRecordList(@ApiParam("当前页码") @PathVariable("currentPage") Integer currentPage,
                                      @ApiParam("每页大小") @PathVariable("pageSize") Integer pageSize,
                                      @ApiParam("模糊查询内容") @RequestBody(required = false) RecordQuery recordQuery
    ) {
        Page<RepairInfo> page = new Page<>(currentPage, pageSize);
        IPage<RepairInfo> repairInfoIPage = null;
        if (recordQuery.getSelect() != null) {
            Integer select = recordQuery.getSelect();
            String text = recordQuery.getRecordInfo();
            switch (select) {
                case 1:
                    repairInfoIPage = repairRecordService.getListByCarNo(page, text);
                    break;
                case 2:
                    repairInfoIPage = repairRecordService.getListByUnitName(page, text);
                    break;
                default:
                    repairInfoIPage = repairRecordService.getListByRname(page, text);
                    break;
            }
        } else {
            repairInfoIPage = repairRecordService.getList(page);
        }
        long total = page.getTotal();
        List<RepairInfo> repairInfoList = repairInfoIPage.getRecords();
        return Result.OK().data("total", total).data("list", repairInfoList);
    }

    @ApiOperation("获取所有的车牌号，修车厂，经办人")
    @GetMapping("/getList")
    public Result getAddList() {
        return Result.OK().data("carList", carService.list()).data("unitList", currentUnitService.list()).data("usersList", qUsersService.list());
    }

    @ApiOperation("保存维修记录")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@ApiParam("前段获取的数据") @RequestBody(required = true) RepairRecord repairRecord) {
        boolean b = false;
        if(repairRecord.getGetTime()==null){
            b=repairRecordService.save(repairRecord);
            carService.setStatesToRepaired(repairRecord.getCarId());
        }else {
            b = repairRecordService.updateById(repairRecord);
            carService.setStatesToUseable(repairRecord.getCarId());
        }

        if (b){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @ApiOperation("根据Id获取信息")
    @GetMapping("/getById/{id}")
    public Result getById(@ApiParam("前段获取的Id") @PathVariable("id") Integer id) {

        return Result.OK().data("repairRecord", repairRecordService.getById(id)).data("repairTypeList", dictionaryService.list(null));
    }

    @ApiOperation("根据id删除信息")
    @GetMapping("/remove/{id}")
    public Result remove(@ApiParam("根据id删除维修记录") @PathVariable("id") Integer id) {
        boolean b = repairRecordService.removeById(id);
        if (b){
            return  Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @ApiOperation("根据id获取详细信息")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@ApiParam("维修记录的Id") @PathVariable("id") Integer id) {

        return Result.OK().data("repairInfo", repairRecordService.getDetailById(id));
    }

    @ApiOperation("根据车牌号报表接口")
    @GetMapping("/table")
    public Result tableByCarNo(){
        return Result.OK().data("table",repairRecordService.getTable());
    }

    @ApiOperation("根据车牌Id报表接口")
    @GetMapping("/tableByCarId/{id}")
    public Result tableByCarId(@ApiParam("车牌的id")@PathVariable("id")Integer id){
        return Result.OK().data("table",repairRecordService.getTableByCarId(id));
    }


}
